package com.weng.platform.article.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.web.base.BaseController;
import com.weng.framework.web.model.request.DeleteReqVo;
import com.weng.platform.article.entity.ArticleInfo;
import com.weng.platform.article.model.request.ArticleInfoReq;
import com.weng.platform.article.model.vo.ArticleInfoDetailVo;
import com.weng.platform.article.model.vo.ArticleInfoEditVo;
import com.weng.platform.article.service.IArticleInfoService;
import com.weng.platform.platform.entity.Column;
import com.weng.platform.platform.service.IColumnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;


/**
 * 文章基本信息表 前端控制器
 *
 * @author wenghui
 * @date 2019-11-30 10:48:30
 *
 */  
@Slf4j
@RestController
@RequestMapping("/article-info")
@Api(value = "文章基本信息表api", tags = {"文章基本信息表操作接口"})
public class ArticleInfoApi extends BaseController {
    @Autowired
    private IArticleInfoService articleInfoService;
    @Autowired
    private IColumnService columnService;

    /**
     * 功能描述:单个获取文章基本信息表
     */
    @ApiOperation(value = "获取文章详细信息", notes = "")
    @GetMapping(value = "/get")
    public ResponseData get(String id){
		ArticleInfoDetailVo articleInfo = articleInfoService.getArticleInfoDetail(id);
        return success(articleInfo);
    }


    /**
     * 条件 分页查询文章基本信息表
     */
    @ApiOperation(value = "分页获取文章基本信息表", notes = "")
    @GetMapping(value = "/list")
    public ResponseData list(Integer pageNo,Integer pageSize, String title, String columnId, String columnCode
						,Date startDate,Date endDate ){
		ArticleInfoReq paramData = new ArticleInfoReq();
		paramData.setTitle(title);
		paramData.setColumnId(columnId);
		paramData.setColumnCode(columnCode);
		paramData.setStartDate(startDate);
		paramData.setEndDate(endDate);

		IPage<ArticleInfoDetailVo> iPage = articleInfoService.list(pageNo, pageSize, paramData);
        return success(iPage);
    }


    /**
	 * 新增文章基本信息表
	 */
	@ApiOperation(value = "新增文章基本信息表", notes = "")
	@PostMapping(value = "/add")
	public ResponseData add(@Valid @RequestBody ArticleInfoEditVo articleInfoReq){
		ArticleInfo articleInfo = articleInfoService.add(articleInfoReq);
		return success(articleInfo);
	}

	/**
	 * 更新文章基本信息表
	 */
	@ApiOperation(value = "更新文章基本信息表", notes = "")
	@PostMapping(value = "/update")
	public ResponseData update(@Valid @RequestBody ArticleInfoEditVo articleInfoReq){
		articleInfoService.update(articleInfoReq);
		return ResponseData.success();
	}


    /**
     * 新增文章基本信息表
     */
    @ApiOperation(value = "基于栏目-新增文章基本信息表", notes = "")
    @PostMapping(value = "/add-by-column")
    public ResponseData addByColumn(@Valid @RequestBody ArticleInfoEditVo articleInfoReq){
        if(StringUtils.isEmpty(articleInfoReq.getColumnCode())){
            return ResponseData.failed("栏目编码不能为空!");
        }
        Column column = columnService.findByCode(articleInfoReq.getColumnCode());
        if(column == null){
            return ResponseData.failed("栏目编码不存在!");
        }
        articleInfoReq.setColumnId(column.getId());
        articleInfoService.add(articleInfoReq);
        return success();
    }

    /**
     * 更新文章基本信息表
     */
    @ApiOperation(value = "基于栏目-更新文章基本信息表", notes = "")
    @PostMapping(value = "/update-by-column")
    public ResponseData updateByColumn(@Valid @RequestBody ArticleInfoEditVo articleInfoReq){
        if(StringUtils.isEmpty(articleInfoReq.getColumnCode())){
            return ResponseData.failed("栏目编码不能为空!");
        }
        Column column = columnService.findByCode(articleInfoReq.getColumnCode());
        if(column == null){
            return ResponseData.failed("栏目编码不存在!");
        }
        articleInfoReq.setColumnId(column.getId());
        articleInfoService.update(articleInfoReq);
        return ResponseData.success();
    }


	/**
	 * 删除文章基本信息表
	 */
	@ApiOperation(value = "ids 删除文章基本信息表", notes = "可以删除多个，中间以,号分隔")
	@PostMapping(value = "/delete")
	public ResponseData delete(@RequestBody DeleteReqVo deleteReq){
		String ids = deleteReq.getIds();
		if(StringUtils.isEmpty(ids)){
			return failed("删除失败,id不能为空！");
		}
		boolean flag = articleInfoService.removeByIds(Arrays.asList(ids.split(",")));
		if(flag){
			return success(deleteReq.getIds());
		}else{
			return failed("删除失败");
		}
	}
}
